home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_005 / speech.demo / speech.demo.c < prev   
C/C++ Source or Header  |  1992-05-06  |  7KB  |  243 lines

  1. /*
  2.  *
  3.  *    DISCLAIMER:
  4.  *
  5.  *    This program is provided as a service to the programmer
  6.  *    community to demonstrate one or more features of the Amiga
  7.  *    personal computer.  These code samples may be freely used
  8.  *    for commercial or noncommercial purposes.
  9.  * 
  10.  *     Commodore Electronics, Ltd ("Commodore") makes no
  11.  *    warranties, either expressed or implied, with respect
  12.  *    to the program described herein, its quality, performance,
  13.  *    merchantability, or fitness for any particular purpose.
  14.  *    This program is provided "as is" and the entire risk
  15.  *    as to its quality and performance is with the user.
  16.  *    Should the program prove defective following its
  17.  *    purchase, the user (and not the creator of the program,
  18.  *    Commodore, their distributors or their retailers)
  19.  *    assumes the entire cost of all necessary damages.  In 
  20.  *    no event will Commodore be liable for direct, indirect,
  21.  *    incidental or consequential damages resulting from any
  22.  *    defect in the program even if it has been advised of the 
  23.  *    possibility of such damages.  Some laws do not allow
  24.  *    the exclusion or limitation of implied warranties or
  25.  *    liabilities for incidental or consequential damages,
  26.  *    so the above limitation or exclusion may not apply.
  27.  *
  28.  */
  29.  
  30. /* speech.demo.c ... as its name implies....  */
  31.  
  32. /* author:  Rob Peck, 12/1/85
  33.  *
  34.  * This code may be freely utilized to create programs for the Amiga. 
  35.  */
  36.  
  37.  
  38. #include "exec/types.h"
  39. #include "exec/exec.h"
  40.  
  41. #include "exec/nodes.h"
  42. #include "exec/lists.h"
  43. #include "exec/memory.h"
  44. #include "exec/interrupts.h"
  45. #include "exec/ports.h"
  46. #include "exec/libraries.h"
  47. #include "exec/io.h"
  48. #include "exec/tasks.h"
  49. #include "exec/execbase.h"
  50.  
  51. #include "devices/narrator.h"
  52. #include "libraries/translator.h"
  53.  
  54. struct MsgPort *readport=0;
  55. struct MsgPort *writeport=0;
  56.  
  57. extern struct MsgPort *CreatePort();
  58. extern struct IORequest *CreateExtIO();    
  59.  
  60. struct narrator_rb *writeNarrator=0;
  61. struct mouth_rb *readNarrator=0;
  62. struct Library *TranslatorBase=0;
  63. UBYTE *sampleinput;        /* pointer to sample input string */
  64. UBYTE outputstring[500];    /* place to put the translation */
  65. SHORT rtnCode;            /* return code from function */
  66. SHORT readError;
  67. SHORT writeError;
  68. SHORT error;
  69. BYTE  audChanMasks[4] = { 3,5,10,12 }; /* which channels to use */
  70.  
  71. #define CANT_OPEN_TRANSLATOR -100
  72. #define CANT_OPEN_NARRATOR -200
  73. #define CREATE_PORT_PROBLEMS -300
  74. #define CREATE_IO_PROBLEMS -400
  75. #define CANT_PERFORM_WRITE -500
  76. #define REVISION 1
  77.  
  78. extern struct Library *OpenLibrary();
  79.  
  80. main()
  81. {
  82.     TranslatorBase = OpenLibrary("translator.library",REVISION);
  83.     if(TranslatorBase == NULL) exit (CANT_OPEN_TRANSLATOR);
  84.     sampleinput = "this is a test"; /* a test string of 14 characters */
  85.     rtnCode = Translate(sampleinput,14,outputstring,500);
  86.     error = rtnCode + 100;
  87.     if(rtnCode != 0) goto cleanup0;
  88.     
  89.     writeport = CreatePort(0,0);
  90.     if(writeport == NULL) { error=CREATE_PORT_PROBLEMS; goto cleanup1; }
  91.     readport = CreatePort(0,0);
  92.     if(readport == NULL) { error=CREATE_PORT_PROBLEMS; goto cleanup2; }
  93.     writeNarrator = (struct narrator_rb *)CreateExtIO(writeport,
  94.                     sizeof(struct narrator_rb));
  95.     if(writeNarrator == NULL) { error=CREATE_IO_PROBLEMS; goto cleanup3; }
  96.     readNarrator = (struct mouth_rb *)CreateExtIO(readport,
  97.                     sizeof(struct mouth_rb));
  98.  
  99.     if(readNarrator == NULL) { error=CREATE_IO_PROBLEMS; goto cleanup4; }
  100. /* SET UP THE PARAMETERS FOR THE WRITE-MESSAGE TO THE NARRATOR DEVICE */
  101.  
  102.     /* show where to find the channel masks */
  103.     writeNarrator->ch_masks = (audChanMasks);
  104.  
  105.     /* and tell it how many of them there are */
  106.     writeNarrator->nm_masks = sizeof(audChanMasks);    
  107.  
  108.     /* tell it where to find the string to speak */    
  109.     writeNarrator->message.io_Data = (APTR)outputstring;
  110.  
  111.     /* tell it how many characters the translate function returned */
  112.     writeNarrator->message.io_Length = strlen(outputstring);
  113.  
  114.     /* if nonzero, asks that mouths be calculated during speech */
  115.     writeNarrator->mouths = 1;
  116.  
  117.     /* tell it this is a write-command */
  118.     writeNarrator->message.io_Command = CMD_WRITE;
  119.  
  120. /* Open the device  */
  121.  
  122.     error = OpenDevice("narrator.device", 0, writeNarrator, 0);
  123.     if(error != 0) goto cleanup4;
  124.  
  125. /* SET UP THE PARAMETERS FOR THE READ-MESSAGE TO THE NARRATOR DEVICE */
  126.  
  127.     /* tell narrator for whose speech a mouth is to be generated */
  128.     readNarrator->voice.message.io_Device = 
  129.         writeNarrator->message.io_Device;
  130.     readNarrator->voice.message.io_Unit = 
  131.         writeNarrator->message.io_Unit;
  132.  
  133.     readNarrator->width = 0;
  134.     readNarrator->height = 0;    /* initial mouth parameters */
  135.  
  136.     readNarrator->voice.message.io_Command = CMD_READ;
  137.         /* initial error value */
  138.     readNarrator->voice.message.io_Error = 0;    
  139.  
  140. /* Send an asynchronous write request to the device */
  141.  
  142.     writeError = SendIO(writeNarrator);
  143.     if(writeError != NULL) { error=CANT_PERFORM_WRITE; goto cleanup5; }
  144.     /* return immediately, run tasks concurrently */
  145.  
  146. /* keep sending reads until it comes back saying "no write in progress" */
  147.  
  148.     while((readError = readNarrator->voice.message.io_Error) != 
  149.         ND_NoWrite)
  150.     {
  151.         DoIO(readNarrator);
  152.         /* put task to sleep waiting for a different
  153.          * mouth shape or return of the message block
  154.          * with the error field showing no write in
  155.          * process
  156.          */
  157.         DrawMouth(readNarrator->width,readNarrator->height);
  158.         /* user's own unique routine, not provided here */
  159.     }
  160.  
  161.     Delay(30);
  162.  
  163.     rtnCode = Translate("No it is not",13,outputstring,500);
  164.     writeNarrator->sex = FEMALE;
  165.     writeNarrator->pitch = MAXPITCH;  /* raise pitch from default value */
  166.     writeNarrator->message.io_Data = (APTR)outputstring;
  167.     writeNarrator->message.io_Length = strlen(outputstring);
  168.     DoIO(writeNarrator);
  169.     
  170.     Delay(30);
  171.  
  172.     rtnCode = Translate("Please! I am speaking now!",26,outputstring,500);
  173.     writeNarrator->sex = MALE;
  174.     writeNarrator->pitch = DEFPITCH;
  175.     writeNarrator->message.io_Data = (APTR)outputstring;
  176.     writeNarrator->message.io_Length = strlen(outputstring);
  177.     DoIO(writeNarrator);
  178.  
  179.     Delay(30);
  180.  
  181.     rtnCode = Translate(
  182.         "Well, you are not very interesting, so I am going home!", 
  183.         55,outputstring,500);
  184.     writeNarrator->sex = FEMALE;
  185.     writeNarrator->pitch = MAXPITCH;
  186.     writeNarrator->message.io_Data = (APTR)outputstring;
  187.     writeNarrator->message.io_Length = strlen(outputstring);
  188.     DoIO(writeNarrator);
  189.  
  190.     Delay(30);
  191.  
  192.     rtnCode = Translate("Bye Bye",7,outputstring,500);
  193.     writeNarrator->sex = MALE;
  194.     writeNarrator->pitch = DEFPITCH;
  195.     writeNarrator->rate = 7;    /* slow him down */
  196.     writeNarrator->message.io_Data = (APTR)outputstring;
  197.     writeNarrator->message.io_Length = strlen(outputstring);
  198.     DoIO(writeNarrator);
  199.  
  200.     cleanup5:
  201.     if(writeNarrator != 0)
  202.         CloseDevice(writeNarrator);
  203.                 /* terminate access to the device */
  204.  
  205.     /* now return system memory to the memory allocator */ 
  206.  
  207.     cleanup4:
  208.     if(readNarrator != 0)
  209.         DeleteExtIO(readNarrator,sizeof(struct mouth_rb));
  210.     cleanup3:
  211.     if(writeNarrator != 0)
  212.         DeleteExtIO(writeNarrator,sizeof(struct narrator_rb));
  213.     cleanup2:
  214.     if(readport != 0)
  215.         DeletePort(readport);
  216.     cleanup1:
  217.     if(writeport != 0)
  218.         DeletePort(writeport);
  219.     cleanup0:
  220.     if(TranslatorBase != 0)
  221.            CloseLibrary(TranslatorBase);
  222.                 /* terminate access to the library */
  223.     
  224.     if(error != 0) exit(error);
  225. } /* end of test */    
  226.  
  227. DrawMouth(w,h)
  228. SHORT w,h;
  229. {    return(0);    /* dummy routine */    }
  230.  
  231. int strlen(string)
  232. char *string;
  233. {
  234.     int i,length;
  235.     length = -1;
  236.     for(i=0; i<256; i++)    /* 256 characters max length at this time */
  237.         {
  238.         if(*string++ == '\0') { length = i+1; break; };
  239.         }
  240.     return(length);
  241. }
  242.  
  243.